/**
 * 简化测试 - 确定问题所在
 */

import { MastraClient } from '@mastra/client-js';

const mastraClient = new MastraClient({
  baseUrl: 'http://localhost:4111',
});

async function simpleTest() {
  try {
    const agent = mastraClient.getAgent('gameMasterAgent');
    const result = await agent.generateVNext({
      messages: [
        {
          role: 'user',
          content: '请开始一个新的赛博侦探游戏。难度：normal，主题：办公室谋杀案，嫌疑人数量：3。请使用 game-master 工具执行 start_game 操作。'
        }
      ]
    });

    console.log('✅ Agent调用成功');
    console.log('- toolResults 存在:', !!result.toolResults);
    console.log('- toolResults 长度:', result.toolResults?.length);

    if (result.toolResults && result.toolResults.length > 0) {
      const toolResult = result.toolResults[0];
      console.log('- toolResult.payload 存在:', !!toolResult.payload);
      console.log('- toolResult.payload.result 存在:', !!toolResult.payload?.result);
      
      if (toolResult.payload?.result) {
        const gameData = toolResult.payload.result;
        console.log('- gameData.success:', gameData.success);
        console.log('- gameData.data 存在:', !!gameData.data);
        console.log('- gameData.data 类型:', typeof gameData.data);
        
        if (gameData.data) {
          console.log('- gameData.data 键:', Object.keys(gameData.data));
          console.log('✅ 数据访问成功！');
          
          // 测试访问具体数据
          console.log('- sessionId:', gameData.data.sessionId);
          console.log('- caseData 存在:', !!gameData.data.caseData);
          
          if (gameData.data.caseData) {
            console.log('- case 存在:', !!gameData.data.caseData.case);
            console.log('- suspects 存在:', !!gameData.data.caseData.suspects);
            
            if (gameData.data.caseData.case) {
              console.log('- 案件标题:', gameData.data.caseData.case.title);
            }
            
            if (gameData.data.caseData.suspects) {
              console.log('- 嫌疑人数量:', gameData.data.caseData.suspects.length);
            }
          }
          
          return {
            success: true,
            data: gameData.data,
            sessionInfo: gameData.sessionInfo,
            message: '测试成功'
          };
        } else {
          console.log('❌ gameData.data 不存在');
          return { success: false, error: 'NO_DATA' };
        }
      } else {
        console.log('❌ toolResult.payload.result 不存在');
        return { success: false, error: 'NO_RESULT' };
      }
    } else {
      console.log('❌ toolResults 不存在或为空');
      return { success: false, error: 'NO_TOOL_RESULTS' };
    }

  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    return { success: false, error: error.message };
  }
}

console.log('🚀 开始简化测试...\n');

simpleTest().then(result => {
  console.log('\n📊 测试结果:', result.success ? '✅ 成功' : '❌ 失败');
  if (!result.success) {
    console.log('错误:', result.error);
  }
}).catch(error => {
  console.error('❌ 测试异常:', error);
});
